package com.pucong.gitee.database.backup.business.unRelational.redis.service;

import com.pucong.gitee.database.backup.business.unRelational.redis.util.*;
import com.pucong.gitee.database.backup.common.entity.redis.RedisConnect;
import com.pucong.gitee.database.backup.common.entity.redis.RedisInfo;
import com.pucong.gitee.database.backup.common.entity.redis.vo.RedisGetKeyInfo;
import com.pucong.gitee.database.backup.common.entity.redis.vo.RedisNetInfo;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.utils.RedisUtil;
import com.pucong.gitee.database.backup.utils.UserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import java.util.*;

@Service
public class InfoClusterServiceImpl implements InfoClusterService {

    @Autowired
    private AutoRedisConnectService autoRedisConnectService;

    @Override
    public RedisInfo getBaseInfo(String id) {
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            RedisInfo redisInfo = RedisUtil.getRedisInfoList(jedis);
            return redisInfo;
        }
        return null;
    }

    @Override
    public List<ClusterRelation> getNodeInfo(String id) {
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            return ClusterUtil.getClusterRelation(jedis.clusterNodes());
        }
        return null;
    }

    @Override
    public RedisNetInfo getMemInfo(String id) {
        RedisNetInfo redisNetInfo = new RedisNetInfo();
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            String redisInfo = RedisUtil.getInfo(jedis, "memory");
            String[] memory = redisInfo.split("\n");
            String value01 = RedisStringUtil.getValueString(":", memory[1]).replace("\r", "");
            String value02 = RedisStringUtil.getValueString(":", memory[4]).replace("\r", "");
            redisNetInfo.setFull(Float.valueOf(value01));
            redisNetInfo.setUsed(Float.valueOf(value02));
        } else {
            redisNetInfo.setFull(0F);
            redisNetInfo.setUsed(0F);
        }
        return redisNetInfo;
    }

    @Override
    public RedisNetInfo getCpuInfo(String id) {
        Jedis jedis = autoRedisConnectService.getJedis(id);
        RedisNetInfo redisNetInfo = new RedisNetInfo();
        if (null != jedis) {
            String redisInfo = RedisUtil.getInfo(jedis, "cpu");
            String[] cpu = redisInfo.split("\n");
            String value01 = RedisStringUtil.getValueString(":", cpu[1]).replace("\r", "");
            String value02 = RedisStringUtil.getValueString(":", cpu[2]).replace("\r", "");
            redisNetInfo.setFull(Float.valueOf(value01));
            redisNetInfo.setUsed(Float.valueOf(value02));
        } else {
            redisNetInfo.setFull(0F);
            redisNetInfo.setUsed(0F);
        }
        return redisNetInfo;
    }

    @Override
    public RedisGetKeyInfo getKeyInfo(String id) {
        RedisGetKeyInfo redisGetKeyInfo = new RedisGetKeyInfo();
        List<String> xdata = new ArrayList<>();
        List<Long> ydata = new ArrayList<>();
        JedisCluster cluster = autoRedisConnectService.getJedisCluster(id);
        RedisConnect redisConnect = autoRedisConnectService.getRedisConnect(id);
        if (null != cluster) {
            List<RedisNode> nodeList = ClusterUtil.getClusterNode(redisConnect);
            Map<String, RedisNode> masterNode = ClusterUtil.getMasterNode(nodeList);
            Map<String, JedisPool> clusterNodes = cluster.getClusterNodes();
            for (String nk : clusterNodes.keySet()) {
                if (masterNode.keySet().contains(nk)) {
                    Jedis jedis = clusterNodes.get(nk).getResource();
                    xdata.add(nk.split(":")[1]);
                    ydata.add(jedis.dbSize());
                    jedis.close();
                }
            }
            redisGetKeyInfo.setXdata(xdata);
            redisGetKeyInfo.setYdata(ydata);
        } else {
            redisGetKeyInfo.setXdata(Collections.EMPTY_LIST);
            redisGetKeyInfo.setYdata(Collections.EMPTY_LIST);
        }

        return redisGetKeyInfo;
    }

    @Override
    public RedisNetInfo getNetInfo(String id) {
        RedisNetInfo redisNetInfo = new RedisNetInfo();
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            String redisInfo = RedisUtil.getInfo(jedis, "stats");
            String[] stats = redisInfo.split("\n");
            String value01 = RedisStringUtil.getValueString(":", stats[6]).replace("\r", "");
            String value02 = RedisStringUtil.getValueString(":", stats[7]).replace("\r", "");
            redisNetInfo.setFull(Float.valueOf(value01));
            redisNetInfo.setUsed(Float.valueOf(value02));
        } else {
            redisNetInfo.setFull(0F);
            redisNetInfo.setUsed(0F);
        }
        return redisNetInfo;
    }
}
